package com.tdengine.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taosdata.jdbc.SchemalessWriter;
import com.taosdata.jdbc.enums.SchemalessProtocolType;
import com.taosdata.jdbc.enums.SchemalessTimestampType;
import com.tdengine.entity.TestEntity;
import com.tdengine.mapper.TDTableMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Api(tags = {"子表管理"})
@RestController
@RequestMapping("table")
public class TDTableController {

    @Autowired
    TDTableMapper tdTableMapper;

//    @ApiOperation("新增单条数据")
//    @GetMapping("insert")
//    public String insert(TestEntity entity){
//        String idStr = IdWorker.getIdStr();
//        entity.setTs(new Date());
//        entity.setSerialNumber(idStr);
//        entity.setX(1.0);
//        entity.setY(2.0);
//        entity.setZ(3.0);
//        entity.setPackageId(IdWorker.get32UUID());
//        entity.setPackageNum(1);
//        tdTableMapper.insertOne(entity);
//        return "成功";
//    }


    /**
     * 提供获取时间的方法
     */
    public static Long localDateTimeToTimestampUs(LocalDateTime localDateTime) {
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        return instant.getEpochSecond() * 1000L + (long)(instant.getNano() / 1000);
    }

    @ApiOperation("批量新增")
    @GetMapping("batchInsert")
    public String batchInsert(String dbName,String supperTableName) throws InterruptedException {
        String idStr = IdWorker.getIdStr();

        List<TestEntity> list=new ArrayList<>();
        Long aLong = localDateTimeToTimestampUs(LocalDateTime.now());
        for (int i=0;i<1000;i++){
            TestEntity entity=new TestEntity();
            aLong+=1;
            System.out.println(aLong);
            entity.setTs(new Date(aLong));
            entity.setSerialNumber(idStr);
            entity.setX(1.0);
            entity.setY(2.0);
            entity.setZ(3.0);
            entity.setPackageNum(i);
            entity.setPackageId(IdWorker.get32UUID());
            entity.setProductId(i);
//            TimeUnit.NANOSECONDS.sleep(1);
            list.add(entity);
        }
        tdTableMapper.batchInsert(idStr,dbName,supperTableName,list);
        return "成功";
    }

    /**
     * @param dataSource：目标数据源
     * @param pageNum：页码
     * @param pageSize：条数
     * @param dbName：数据库名
     * @param tableName：表名
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("page")
    public Page<TestEntity> page(int pageNum,int pageSize,String dbName,String tableName){
        Page<TestEntity> page=new Page<>(pageNum,pageSize);
        //动态设置ThreadLocal的值
        List<TestEntity> selectPage = tdTableMapper.page(page,dbName,tableName);
        page.setRecords(selectPage);
        return page;
    }
    /*
    int转时间
       int a=1699863009;
        long b= (long)a * 1000;// 将int转换为long
        Date date = new Date(b);
        Timestamp timestamp = new Timestamp(b);
        System.out.println(timestamp);
        System.out.println(date);
     */

}
